// 帮助与反馈
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    help:[ ],
    question:[],
    //当前的提问
    nowQuest:"",
    color:"",
    req: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var params = {
      pagenum: 1,
      listNum: 4
    }
    var that = this;
    app.getRequest("api/carmation/selectMation", params, function (e) {
      if (e.code == 200) {
        var data = e.data;
        var help = data.list;
        that.setData({
          help: help
        })
      } else {
        wx.showToast({
          title: e.message,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getUserInfoFun();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 问题详情
   */
  detail:function(e){
    wx.navigateTo({
      url: "/help/questDetail/questDetail?id=" + e.currentTarget.dataset.id
    })
  },
  /**
   * 更多问题
   */
  more: function(){
    wx.navigateTo({
      url: '/help/always/always',
    })
  },
  /**
   * 输入问题
   */
  input: function(e){
    var question = e.detail.value;
    this.setData({
      nowQuest: question
    })
  },
  /**
   * 提问
   */
  send: function(){
    var that = this;
    var newQuest = this.data.nowQuest;
    var question = this.data.question;
    var data = new Date();
    if (newQuest == ""){
      this.setData({
        color: "red"
      })
      return;
    }
    var now = data.toLocaleString();
    var newQ = {
      question: newQuest,
      time: now
    }
   
    var user = wx.getStorageSync("user");
    var name = user.name;
    console.log(user);
    if (name == "" || !/^\w{11}$/.test(user.phone)){
      wx.showModal({
        title: '友情提醒！',
        content: '请完善合法的个人信息！',
        success:(res)=>{
          if (res.confirm){
            wx.navigateTo({
              url: '/person/userInfo/userInfo',
            })
          }
        }
      })
    } else {
      var params = {
        name: name,
        content: newQuest,
        phone: user.phone,
        "type": 1,
        mix_id: user.u_id
      }
      app.getRequest("api/user/addfeedback",params,function(e){
        if(e.code == 200) {
          question.push(newQ);
          that.setData({
            question: question,
            newQuest: ""
          })
        } else {
          wx.showToast({
            title: e.message,
            icon: "none"
          })
        }
      })
    }



  }
})